/********************************************************************************
* @file    app_flash.c
* @author  jianqiang.xue
* @version V1.0.0
* @date    2021-11-12
* @brief   [业务]flash内容管理
********************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include <stdint.h>
#include <stdbool.h>
#include <string.h>

#include "os_api.h"
#include "log.h"
#include "bsp_flash.h"
#include "kv_sys.h"

/* Private Includes ----------------------------------------------------------*/

#include "app_main.h"
#include "ls_gpio.h"
#include "ls_syscfg.h"
#include "app_flash.h"

/* Private Define ------------------------------------------------------------*/
/* External Variables --------------------------------------------------------*/
/* Private Macro -------------------------------------------------------------*/
/* Private Variables ---------------------------------------------------------*/
/* Private Function Prototypes -----------------------------------------------*/

/**
 * @brief  从FLASH读取boot信息配置
 */
bool flash_read_boot_info(void) {
    uint8_t* p;
    p = kv_get_env(LS_KV_KEY_BOOT_INFO);
    if (p != NULL)
        memcpy((uint8_t*)&g_boot_info, p, sizeof(boot_info_t));
    else
        memset(&g_boot_info, 0, sizeof(boot_info_t));
    return true;
}

bool flash_read_user_info(void) {
    uint8_t* p;
    p = kv_get_env(LS_KV_KEY_USER_INFO);
    if (p != NULL)
        memcpy((uint8_t*)&system_info, p, sizeof(system_info_t));
//    else
//        memset(&system_info, 0, sizeof(system_info_t));
    return true;
}

/**
 * @brief  写配置信息到FLASH
 */
bool flash_write_sys_cfg(void) {
    if (system_info.write_in) {
        system_info.write_in = false;
        main_send_signal(SIGNAL_KV_WRITE_SYS_INFO);
        return true;
    }
    return false;
}
